diff options
Diffstat (limited to 'frontend/app/download/[...path]/route.ts')
| -rw-r--r-- | frontend/app/download/[...path]/route.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/frontend/app/download/[...path]/route.ts b/frontend/app/download/[...path]/route.ts new file mode 100644 index 0000000..966a89e --- /dev/null +++ b/frontend/app/download/[...path]/route.ts | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | import { NextRequest, NextResponse } from 'next/server' | ||
| 2 | import { Drive_stat } from '@/lib/drive_server' | ||
| 3 | |||
| 4 | // GET /download/[...path] - Download file by path (redirects to blob endpoint) | ||
| 5 | export async function GET( | ||
| 6 | request: NextRequest, | ||
| 7 | { params }: { params: Promise<{ path: string[] }> } | ||
| 8 | ) { | ||
| 9 | try { | ||
| 10 | const { path } = await params | ||
| 11 | |||
| 12 | // Reconstruct the full path | ||
| 13 | const fullPath = '/' + path.join('/') | ||
| 14 | |||
| 15 | // Get filename from path for the download | ||
| 16 | const filename = path[path.length - 1] || 'download' | ||
| 17 | |||
| 18 | // Get blob ID using Drive_stat | ||
| 19 | const blobId = await Drive_stat(fullPath) | ||
| 20 | |||
| 21 | // Redirect to blob endpoint with filename - preserve original host | ||
| 22 | // Use X-Forwarded-Host or Host header to get the correct host | ||
| 23 | const forwardedHost = request.headers.get('x-forwarded-host') | ||
| 24 | const host = forwardedHost || request.headers.get('host') || new URL(request.url).host | ||
| 25 | const protocol = request.headers.get('x-forwarded-proto') || (new URL(request.url).protocol.replace(':', '')) | ||
| 26 | const redirectUrl = `${protocol}://${host}/blob/${blobId}?filename=${encodeURIComponent(filename)}` | ||
| 27 | |||
| 28 | return NextResponse.redirect(redirectUrl) | ||
| 29 | |||
| 30 | } catch (error) { | ||
| 31 | console.error('Download error:', error) | ||
| 32 | return new NextResponse( | ||
| 33 | error instanceof Error ? error.message : 'File not found', | ||
| 34 | { status: 404 } | ||
| 35 | ) | ||
| 36 | } | ||
| 37 | } \ No newline at end of file | ||
